<template>
    <div class="app-container">

        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

            <el-form-item label="OPEN_ID" prop="openId">
                <el-input v-model="queryParams.openId" placeholder="请输入OPEN_ID" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item label="昵称" prop="nickName">
                <el-input v-model="queryParams.nickName" placeholder="请输入昵称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item label="电话" prop="phone">
                <el-input v-model="queryParams.phone" placeholder="请输入电话" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item label="状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
                    <el-option v-for="dict in dict.type.self_yes_no_success" :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
            </el-form-item>

            <el-form-item label="禁用" prop="disabled">
                <el-select v-model="queryParams.disabled" placeholder="请选择禁用状态" clearable>
                    <el-option v-for="dict in dict.type.self_yes_no_danger" :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
            </el-form-item>

            <el-form-item label="删除" prop="del">
                <el-select v-model="queryParams.del" placeholder="请选择删除状态" clearable>
                    <el-option v-for="dict in dict.type.self_yes_no_danger" :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>

        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['self:wechat:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['self:wechat:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['self:wechat:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['self:wechat:export']">导出</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-upload" size="mini" @click="handleImport" v-hasPermi="['self:wechat:import']">导入</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="ID" align="center" prop="id" />
            <el-table-column label="OPEN_ID" align="center" prop="openId" />
            <el-table-column label="昵称" align="center" prop="nickName" />
            <el-table-column label="头像" align="center">
                <template slot-scope="scope">
                    <ImagePreview :src="scope.row.headImg" :width="100" :height="50" v-if="scope.row.headImg" />
                </template>
            </el-table-column>
            <el-table-column label="电话" align="center" prop="phone" />
            <el-table-column label="状态" align="center">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.self_yes_no_success" :value="scope.row.status" />
                </template>
            </el-table-column>
            <el-table-column label="禁用" align="center">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.self_yes_no_danger" :value="scope.row.disabled" />
                </template>
            </el-table-column>
            <el-table-column label="删除" align="center">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.self_yes_no_danger" :value="scope.row.del" />
                </template>
            </el-table-column>
            <el-table-column label="资质" align="center" prop="zz" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['self:wechat:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['self:wechat:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改对话框 -->
        <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="OPEN_ID" prop="openId">
                    <el-input v-model="form.openId" placeholder="请输入OPEN_ID" maxlength="64" clearable />
                </el-form-item>
                <el-form-item label="昵称" prop="nickName">
                    <el-input v-model="form.nickName" placeholder="请输入昵称" maxlength="64" clearable />
                </el-form-item>
                <el-form-item label="电话" prop="phone">
                    <el-input v-model="form.phone" placeholder="请输入电话" maxlength="11" clearable />
                </el-form-item>
                <el-form-item label="密码" prop="pwd" v-if="!form.id">
                    <el-input v-model="form.pwd" placeholder="请输入密码" type="password" maxlength="64" clearable />
                </el-form-item>
                <el-form-item label="头像" prop="headImg">
                    <ImageUpload v-model="form.headImg" :limit="1" :fileType="['png', 'jpg', 'jpeg']" :isShowTip="false" v-if="open"></ImageUpload>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-select v-model="form.status" placeholder="请选择状态">
                        <el-option v-for="dict in dict.type.self_yes_no_success" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="禁用" prop="disabled">
                    <el-radio-group v-model="form.disabled">
                        <el-radio v-for="dict in dict.type.self_yes_no_danger" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="删除" prop="del">
                    <el-radio-group v-model="form.del">
                        <el-radio v-for="dict in dict.type.self_yes_no_danger" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="资质" prop="zz">
                    <FileUpload v-model="form.zz" :limit="1" :fileType='["jpg", "doc", "xls", "ppt", "txt", "pdf"]' :isShowTip="false" v-if="open">
                    </FileUpload>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- 导入对话框组件 -->
        <ImportExcel ref="importComponent" @import-success="resetQuery"></ImportExcel>
    </div>
</template>

<script>
import { queryList, getInfo, remove, save, update } from "@/api/self/wechat";

export default {
    name: "WechatUser",
    dicts: ["self_yes_no_success", "self_yes_no_danger"],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            dataList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                openId: undefined,
                nickName: undefined,
                phone: undefined,
                status: undefined,
                disabled: undefined,
                del: undefined,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                openId: [
                    { required: true, message: "OPEN_ID不能为空", trigger: "blur" }
                ],
                nickName: [
                    { required: true, message: "昵称不能为空", trigger: "blur" }
                ],
                phone: [
                    { required: true, pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur" }
                ],
                pwd: [
                    { required: true, message: "密码不能为空", trigger: "blur" }
                ],
            },
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询列表 */
        getList() {
            this.loading = true;
            queryList(this.queryParams).then((response) => {
                this.dataList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                openId: undefined,
                nickName: undefined,
                phone: undefined,
                pwd: undefined,
                headImg: undefined,
                status: '是',
                disabled: '否',
                del: '否',
                zz: undefined
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids[0];
            getInfo({ id }).then((response) => {
                this.form = response.data;
                this.open = true;
                this.title = "修改";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        update(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        save(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id ? [row.id] : this.ids;
            this.$modal
                .confirm('是否确认删除编号为"' + ids + '"的数据项？')
                .then(function () {
                    return remove({ ids });
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => { });
        },
        /** 导出文件 */
        handleExport() {
            this.$download.downloadFile({
                url: process.env.VUE_APP_BASE_API + '/wechat/user/export',
                method: 'post',
                fileName: "外部用户信息列表.xlsx",
                data: this.queryParams
            });
        },
        handleImport() {//导入
            const imps = this.$refs.importComponent;
            if (imps) {
                //设置导入参数
                imps.action = process.env.VUE_APP_BASE_API + '/wechat/user/import';//导入接口地址
                imps.showDownTemplate = false;//是否显示 模板下载 按钮： false:不显示 true:显示(必须配置模板的下载地址)
                imps.downTemplateUrl = process.env.VUE_APP_BASE_API + '';//模板下载 地址
                imps.downTemplateMethod = 'get';//模板下载 方式 [get|post]
                imps.downTemplateParam = {};//模板下载 参数
                //显示弹框
                imps.visible = true;
            }
        },
    },
};
</script>